package com.ly.houses.service.impl;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ly.houses.common.HttpUtil;
import com.ly.houses.mapper.LoginMapper;
import com.ly.houses.model.Result;
import com.ly.houses.model.ResultUtil;
import com.ly.houses.model.User;
import com.ly.houses.service.LoginService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private LoginMapper loginMapper;

    public static Map<String, Object> getOpenIdAndUnionIdAndSessionKeyByCode(String code) throws JsonParseException, JsonMappingException, IOException {
        Map<String, Object> result = new HashMap<>();
        String url =  "https://api.weixin.qq.com/sns/jscode2session?appid=wx45ecbf2b3bd2945e&secret=a3b768f0c5b169af5d690c1913c13171&js_code=" + code + "&grant_type=authorization_code";
        String data = HttpUtil.HttpPostWithJson(url);
        ObjectMapper mapper = new ObjectMapper();
        result = mapper.readValue(data, Map.class);
        return result;
    }
    @Override
    public Result getOpenId(HttpSession session, String code) {
        Map<String, Object> map = null;
        Map<String, Object> result = new HashMap<>();
        try {
            map = getOpenIdAndUnionIdAndSessionKeyByCode(code);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(7778, "网络故障");
        }
        if (null != map && map.size() > 0) {
            String openId = (String) map.get("openid");
            if (!StringUtils.isEmpty(openId)) {
                result.put("sessionId", session.getId());
                User  user=loginMapper.getByOpenId(openId);
                if(user==null){
                    loginMapper.save(openId);
                    user=loginMapper.getByOpenId(openId);
                }
                session.setAttribute("user", user);
                return ResultUtil.success(result);
            } else {
                return ResultUtil.error(7008, "授权失败");
            }
        }
        return ResultUtil.error(7778, "网络故障");
    }

//    @Override
//    public Result authorize(HttpSession session, String openId, String phone) {
//        User user = loginMapper.getByPhone(phone);
//        if (user == null) {
//            return ResultUtil.error(8888, "无此用户");
//        }
//        if (StringUtils.isEmpty(user.getOpenId())) {
//            Integer res = loginMapper.updateOpenId(openId, user.getId());
//            if (res == 1) {
//                user.setOpenId(openId);
//                session.setAttribute("user", user);
//                return ResultUtil.success(session.getId());
//            }
//            return ResultUtil.error(7777, "操作失败，请重试");
//        }
//        return ResultUtil.error(7777, "操作失败，请重试");
//    }
}
